/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.command.component.utils;

import com.fasterxml.jackson.core.JsonGenerator.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;

/**
 * 功能描述:
 *
 * @ClassName: ObjectMapperUtil
 * @Author: Fynn Qi
 * @Date: 2020/1/1 9:51
 * @Version: V1.0
 */
public class ObjectMapperUtil {

    public static ObjectMapper MAPPER;

    static {
        MAPPER = new ObjectMapper();
    }

    public ObjectMapperUtil() {
    }

    public static <T> T getPropValue(JsonNode jObj, String propName, Class<T> tClass) {
        if (jObj == null) {
            return null;
        } else {
            JsonNode propNode = jObj.get(propName);
            if (propNode == null) {
                return null;
            } else {
                try {
                    return MAPPER.treeToValue(propNode, tClass);
                } catch (JsonProcessingException var5) {
                    throw new RuntimeException("获取属性出错：", var5);
                }
            }
        }
    }

    public static <T> T getPropValue(JsonNode jObj, String propName, TypeReference<T> refer) {
        if (jObj == null) {
            return null;
        } else {
            JsonNode propNode = jObj.get(propName);
            return propNode == null ? null : MAPPER.convertValue(propNode, refer);
        }
    }

    public static Boolean isExistsProp(JsonNode jObj, String propName) {
        return jObj == null ? false : jObj.has(propName);
    }

    public static void setPropValue(JsonNode jobj, String propName, Object t) {
        if (jobj != null && t != null) {
            ObjectNode objNode = (ObjectNode)jobj;
            objNode.set(propName, MAPPER.valueToTree(t));
        }
    }

    public static ArrayNode addEmptyArray(JsonNode jobj, String propName) {
        ArrayNode arrayNode = MAPPER.createArrayNode();
        ObjectNode objNode = (ObjectNode)jobj;
        objNode.set(propName, arrayNode);
        return arrayNode;
    }

    public static <T> T deserialize(String string, Class<T> type) {
        try {
            return MAPPER.readValue(string, type);
        } catch (IOException var3) {
            throw new RuntimeException("反序列化时出错：", var3);
        }
    }

    public static String serialize(Object object) {
        try {
            return MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException var2) {
            throw new RuntimeException("序列化时出错：", var2);
        }
    }

    static {
        MAPPER.configure(Feature.IGNORE_UNKNOWN, true);
        MAPPER.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        MAPPER.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        MAPPER.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    }
}
